<template>
  <div>


    <vxe-toolbar ref="toolbarRef" custom>

      <template #buttons>
        <Button type="primary" @click="AddData" icon="md-add">新增</Button>

      </template>
    </vxe-toolbar>
    <vxe-table ref="xTable" border id="system_sysmenu" :height="tableHeight" show-header-overflow show-overflow
      align="center" size="small" :loading="loadingTable" :row-config="{ isCurrent: true, isHover: true }"
      :keyboard-config="{ isArrow: true }"
      :tree-config="{ transform: true, rowField: 'Id', parentField: 'ParentId', iconOpen: 'vxe-icon-square-minus-fill', iconClose: 'vxe-icon-square-plus-fill' }"
      :data="tableData">

      <vxe-column type="seq" title="序号" width="60"></vxe-column>
      <vxe-column field="Title" title="标题" width="200" tree-node></vxe-column>
      <vxe-column field="Type" title="类型" width="100">

        <template #default="{ row }">
          <Tag :color="getMenuColor(row.Type)">{{ getMenuType(row.Type) }}</Tag>
        </template>
      </vxe-column>

      <vxe-column field="PermissionKey" title="权限标识" width="200"></vxe-column>
      <vxe-column field="Name" title="路由名称" width="200"></vxe-column>
      <vxe-column field="Path" title="路由地址" width="300"></vxe-column>
      <vxe-column field="Component" title="组件" min-width="300">

        <template #default="{ row }">
          {{ row.Component }}
        </template>
      </vxe-column>
      <vxe-column field="Icon" title="图标" width="100">

        <template #default="{ row }">
          <Icon :type="row.Icon" size="22"></Icon>
        </template>
      </vxe-column>
      <vxe-column field="Sort" title="序号" width="100"></vxe-column>
      <vxe-column title="操作" fixed="right" width="200">

        <template #default="{ row }">
          <Button type="primary" @click="tempData = row; modalData = true" style="margin-right:5px"
            size="small">修改</Button>
          <Button type="warning" @click="CopyMenu(row.Id)" size="small" style="margin-right:5px">复制</Button>
          <Button type="error" @click="DeleteMenu(row.Id)" size="small">删除</Button>
        </template>
      </vxe-column>
    </vxe-table>

    <Modal :width="800" draggable sticky v-model="modalData" :title="tempData.Id > 0 ? '修改' : '新增'"
      :mask-closable="false">
      <Form ref="formValidate" class="form-more" :model="tempData" :label-width="90" :rules="ruleValidate">
        <Row>
          <Col span="12">
          <FormItem label="标题" prop="Title">
            <Input v-model="tempData.Title" />
          </FormItem>
          </Col>
          <Col span="12">
          <FormItem label="权限标识" prop="PermissionKey">
            <Input v-model="tempData.PermissionKey" />
          </FormItem>

          </Col>
        </Row>
        <FormItem label="上级菜单" prop="ParentId">
          <treeselect v-model="tempData.ParentId" :options="menuTreeList" :clearable="false" :defaultExpandLevel="10" />
        </FormItem>

        <FormItem label="类型" prop="Type">
          <RadioGroup v-model="tempData.Type" type="button" button-style="solid">
            <Radio :label="1">目录</Radio>
            <Radio :label="2">菜单</Radio>
            <Radio :label="3">外链</Radio>
            <Radio :label="4">权限</Radio>
          </RadioGroup>
        </FormItem>
        <Row>
          <Col span="12">
          <FormItem label="路由名称" prop="Name">
            <Input v-model="tempData.Name" />
          </FormItem>
          </Col>
          <Col span="12">
          <FormItem label="路由地址" prop="Path">
            <Input v-model="tempData.Path" />
          </FormItem>
          </Col>
        </Row>

        <FormItem label="组件" prop="Component">
          <Input v-model="tempData.Component" />
        </FormItem>
        <FormItem label="外链地址" prop="Href">
          <Input v-model="tempData.Href" />
        </FormItem>
        <Row>
          <Col span="7">
          <FormItem label="参数支持" prop="Props">
            <i-switch v-model="tempData.Props" />
          </FormItem>
          </Col>
          <Col span="7">
          <FormItem label="参数详情" prop="PropsInfo">
            <Input v-model="tempData.PropsInfo" />
          </FormItem>
          </Col>
          <Col span="7">
          <FormItem label="动态标题" prop="PropsTitle">
            <i-switch v-model="tempData.PropsTitle" />
          </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span="7">
          <FormItem label="隐藏菜单" prop="HideInMenu">
            <i-switch v-model="tempData.HideInMenu" />
          </FormItem>
          </Col>
          <Col span="7">
          <FormItem label="不缓存" prop="NotCache">
            <i-switch v-model="tempData.NotCache" />
          </FormItem>
          </Col>
          <Col span="7">
          <FormItem label="系统菜单" prop="IsSystem">
            <i-switch v-model="tempData.IsSystem" />
          </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span="12">
          <FormItem label="图标" prop="Icon">
            <Input v-model="tempData.Icon" readonly style="cursor: pointer;" @click.native="openIconSele()">
            <Icon :type="tempData.Icon || 'ios-settings-outline'" size="20" slot="append" />
            </Input>
          </FormItem>
          </Col>
          <Col span="12">
          <FormItem label="排序" prop="Sort">
            <Input type="number" v-model="tempData.Sort" />
          </FormItem>
          </Col>
        </Row>

      </Form>
      <div slot="footer">
        <Button type="text" @click="modalData = false">取消</Button>
        <Button type="primary" :loading="loading" @click="SaveMenu">确定</Button>
      </div>
    </Modal>
    <IconChose ref="modalIcon" @chose="selectedIcon" />
  </div>
</template>

<script>
import XEUtils from 'xe-utils'
import IconChose from '_c/IconChose'
// import the component
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { GetMenuList, CopyMenu, DeleteMenu, SaveMenu } from '@/api/system'
import { getMenuType, getMenuColor } from '@/libs/enum'
export default {
  name: 'sysmenu',
  data() {
    return {
      value: '',
      menuTreeList: [],
      tableHeight: 500,
      ruleValidate: {
        Title: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ],
        PermissionKey: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ],
        Type: [
          {
            type: 'number',
            required: true,
            message: '不能为空',
            trigger: 'change'
          }
        ]
      },
      loading: false,
      loadingTable: false,
      modalData: false,
      tempData: {

      },

      tableData: []
    }
  },
  components: {
    IconChose,
    Treeselect
  },
  computed: {},
  methods: {
    getMenuType,
    getMenuColor,

    openIconSele() {
      this.$refs.modalIcon.show()
    },
    selectedIcon(item) {
      this.tempData.Icon = item
    },
    SaveMenu() {
      let vm = this
      vm.$refs['formValidate'].validate((valid) => {
        if (valid) {
          vm.loading = true
          SaveMenu(vm.tempData).then((res) => {
            vm.loading = false

            if (res.StatusCode == 200) {
              vm.modalData = false
              vm.$Message.success({ title: '提示', content: '保存成功' })
              vm.GetMenuList()
            } else {
              vm.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        }
      })
    },
    GetMenuList() {
      this.loadingTable = true
      GetMenuList().then((res) => {
        this.loadingTable = false

        if (res.StatusCode == 200) {
          this.tableData = res.Data
          var treeList = res.Data.map(x => {
            return {
              id: x.Id,
              label: x.Title,
              parentId: x.ParentId,
            }
          })
          treeList.push({
            id: 0,
            label: '顶级',
          })
          this.menuTreeList = XEUtils.toArrayTree(treeList, { strict: true })
          this.$nextTick(() => {
            this.$refs.xTable.setAllTreeExpand(true)
          })


        } else {
          vm.$Notice.error({
            title: data.Errors
          })
        }
      })
    },
    CopyMenu(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要复制吗？',
        onOk: () => {
          CopyMenu(id).then((res) => {
            if (res.StatusCode == 200) {
              this.GetMenuList()
              this.$Message.success({ title: '提示', content: '操作成功' })
            }
          })
        },
        onCancel: () => { }
      })
    },
    DeleteMenu(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要删除吗？',
        onOk: () => {
          DeleteMenu(id).then((res) => {
            if (res.StatusCode == 200) {
              this.GetMenuList()
              this.$Message.success({ title: '提示', content: '删除成功' })
            } else {
              this.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        },
        onCancel: () => { }
      })
    },

    AddData() {
      var vm = this
      vm.modalData = true
      vm.tempData = {
        ParentId: 0,
        Type: 1,
        Icon: ''
      }
    }
  },

  mounted() {
    this.tableHeight = window.innerHeight - 240
    const $table = this.$refs.xTable
    const $toolbar = this.$refs.toolbarRef
    if ($table && $toolbar) {
      $table.connect($toolbar)
    }
    this.GetMenuList()
  }
}
</script>

<style></style>
